<?php

namespace app\common\service;


class HostAccessLog extends Base 

{
    function list($hostId, $params, $limit, $field = '*', $action = 'detail')
    {
        $db = db('host_access_log')->where('host_id', $hostId);

        isset($params['source_ip']) && !empty($params['source_ip']) ?  $db->where('source_ip','like', '%'.$params['source_ip'].'%' ) : '';
        isset($params['direction']) && is_numeric($params['direction']) ?  $db->where('direction', $params['direction']) : '';
        isset($params['target_ip']) && !empty($params['target_ip']) ?  $db->where('target_ip','like', '%'.$params['target_ip'].'%' ) : '';
        isset($params['target_port']) && is_numeric($params['target_port']) ?  $db->where('target_port', $params['target_port']) : '';
        isset($params['protocols']) && is_numeric($params['protocols']) ?  $db->where('protocols', $params['protocols']) : '';
        isset($params['permission']) && is_numeric($params['permission']) ?  $db->where('permission', $params['permission']) : '';

        if (isset($params['first_time']) && !empty($params['first_time'])){
            $firstTime = strtotime($params['first_time']);
            $db->where('first_time','between',[$firstTime, $firstTime + 86400]);
        }

        if (isset($params['last_time']) && !empty($params['last_time'])){
            $lastTime = strtotime($params['last_time']);
            $db->where('last_time','between',[$lastTime, $lastTime + 86400]);
        }

        isset($params['ignore']) && is_numeric($params['ignore']) ?  $db->where('ignore', $params['ignore']) : '';


        if($action == 'list'){
            $list = $db->field($field)->group('source_ip, target_ip')->paginate($limit)->toArray();
        }else{
            $list = $db->field($field)->paginate($limit)->toArray();
        }

        return $list;
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/11/28 9:18
     */
    function modify($condition, $updateData)
    {
        return db('host_access_log')->where($condition)->update($updateData);
    }


    /**
     * @name 获取总数
     * @param $condition
     * @return float|string
     * @author wx
     * @date 2019/11/28 9:20
     */
    function gettotal($condition)
    {
        return db('host_access_log')->where($condition)->count();
    }

    /**
     * @name 获取出栈总条数
     * @param $condition
     * @return float|string
     * @author wx
     * @date 2019/11/28 9:50
     */
    function getTotalOut($condition)
    {
        $condition['direction'] = 1;//出栈
        return db('host_access_log')->where($condition)->count();
    }

    /**
     * @name 入栈总条数         //direction 流量方向 1出站 2进站
     * @param $condition
     * @return float|string
     * @author wx
     * @date 2019/11/28 9:50
     */
    function getTotalInput($condition)
    {
        $condition['direction'] = 2;//入栈
        return db('host_access_log')->where($condition)->count();
    }
}